package com.zfzs.post.service.impl;

import com.alibaba.fastjson.JSON;
import com.zfzs.post.exception.BadRequestException;
import com.zfzs.post.model.dto.TerminalTypeIdDTO;
import com.zfzs.post.model.vo.QueryAddAgentListVO;
import com.zfzs.post.model.vo.StockStateVO;
import com.zfzs.post.model.vo.transaction.TerminalTypePolicyVO;
import com.zfzs.post.modules.agent.domain.Agent;
import com.zfzs.post.modules.agent.service.AgentService;
import com.zfzs.post.modules.domain.Picture;
import com.zfzs.post.modules.policy.domain.PolicyInfo;
import com.zfzs.post.modules.policy.service.PolicyInfoService;
import com.zfzs.post.modules.post.domain.TerminalType;
import com.zfzs.post.modules.post.service.TerminalTypeService;
import com.zfzs.post.modules.post.service.dto.QueryTerminal4DistribuDTO;
import com.zfzs.post.modules.post.service.dto.QueryTerminalListDTO;
import com.zfzs.post.model.vo.TerminalList4DistribuVo;
import com.zfzs.post.modules.agent.service.dto.AgentDTO;
import com.zfzs.post.modules.post.domain.TerminalInfo;
import com.zfzs.post.modules.post.service.TerminalInfoService;
import com.zfzs.post.modules.post.service.mybatis.ITerminalInfoService;
import com.zfzs.post.service.JwtUserDetailsService;
import com.zfzs.post.service.PictureService;
import com.zfzs.post.service.TerminalService;
import com.zfzs.post.utils.CloneUtils;
import com.zfzs.post.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author wq
 * @date 2019/10/14 14:10
 * @description
 */
@Service
public class TerminalServiceImpl implements TerminalService {

    @Autowired
    private TerminalInfoService terminalInfoService;

    @Autowired
    private JwtUserDetailsService jwtUserDetailsService;

    @Autowired
    private TerminalTypeService terminalTypeService;

    @Autowired
    private PictureService pictureService;

    @Autowired
    private PolicyInfoService policyInfoService;

    @Autowired
    private AgentService agentService;

    @Autowired
    ITerminalInfoService IterminalInfoService;

    @Override
    public TerminalList4DistribuVo getTerminalList4Distribu(QueryTerminalListDTO dto) {
        AgentDTO agentInfo = jwtUserDetailsService.getAgentInfo();
        boolean startSnEmpty = StringUtils.isEmpty(dto.getStartSn());
        boolean endSnEmpty = StringUtils.isEmpty(dto.getEndSn());

        if (startSnEmpty != endSnEmpty) {
            throw new BadRequestException("请完整填写起止终端号");
        }

        dto.setChannelId(agentInfo.getChannelId());
        dto.setAgentId(agentInfo.getId());
        List<TerminalInfo> list = terminalInfoService.findTerminalList4Distribu(dto);

        List<QueryTerminal4DistribuDTO> terminalVOS = CloneUtils.batchClone(list, QueryTerminal4DistribuDTO.class);

        TerminalList4DistribuVo vo = new TerminalList4DistribuVo();
        vo.setErrorList(terminalVOS.stream().filter(e -> e.getIsReport().intValue() == 1 || e.getIsActivation().intValue() == 1).collect(Collectors.toList()));
        vo.setNormalList(terminalVOS.stream().filter(e -> e.getIsReport().intValue() == 0 && e.getIsActivation().intValue() == 0).collect(Collectors.toList()));
        return vo;
    }

    @Override
    public Map<String, Object> getTerminalTypeList() {
        Map<String, Object> map = new HashMap<>();
        AgentDTO agentInfo = jwtUserDetailsService.getAgentInfo();
        Long channelId = agentInfo.getChannelId();
        List<TerminalType> list = terminalTypeService.findByChannelId(channelId);
        map.put("list", list);
        return map;
    }

    @Override
    public Map<String, Object> getpolicyTerminalTypeList() {
        Map<String, Object> map = new HashMap<>();
        AgentDTO agentInfo = jwtUserDetailsService.getAgentInfo();
        Long channelId = agentInfo.getChannelId();
        List<TerminalTypePolicyVO> policyList = new LinkedList<>();
        List<TerminalType> list = terminalTypeService.findByChannelId(channelId);
        for (int i = 0; i < list.size(); i++) {
            TerminalTypePolicyVO policyVO = JSON.parseObject(JSON.toJSONString(list.get(i)), TerminalTypePolicyVO.class);
            Long terminalTypeId = list.get(i).getId();
            PolicyInfo policyInfo = new PolicyInfo();
            policyInfo.setTerminalTypeId(terminalTypeId);
            policyInfo.setAgentId(agentInfo.getId());
            PolicyInfo p = policyInfoService.findOne(policyInfo);
            //查询上级人信息
            Agent pidAgent = agentService.findById(agentInfo.getPidId());
            String phone = "";
            if (pidAgent != null) {
                phone = pidAgent.getPhone();
            }
            policyVO.setPidPhone(phone);
            if (p == null) {
                //没有设置的权限
                policyVO.setIsSetting(false);
            } else {
                policyVO.setIsSetting(true);
            }
            policyList.add(policyVO);
        }
        map.put("list", policyList);
        return map;
    }

    @Override
    public void updateFinalOwner(Long agentId, String... disSnList) {
        terminalInfoService.updateTerminalsAgent(agentId, disSnList);
    }

    @Override
    public Map<String, Object> queryStockStateList() {
        Map<String, Object> map = new HashMap<>();
        AgentDTO agentInfo = jwtUserDetailsService.getAgentInfo();
        List<StockStateVO> list = IterminalInfoService.queryStockState(agentInfo.getId(), agentInfo.getChannelId());
        map.put("list", list);
        return map;
    }

    @Override
    public Map<String, Object> getTerminalStatusList() {

        Map<String, Object> result = new HashMap<>();

        result.put("isActivation", new HashMap<String, String>() {{
            put("1", "已激活");
            put("0", "未激活");
        }});

        return result;
    }

    @Override
    public Map<String, Object> getTerminalTypeInfo(TerminalTypeIdDTO dto) {
        Map<String, Object> map = new HashMap<>();
        Long terminalTypeId = dto.getTerminalTypeId();
        TerminalType terminalType = terminalTypeService.findById(terminalTypeId);
        if (terminalType == null) {
            throw new BadRequestException("未查询到终端类型信息");
        }
        map.put("depositAmount", terminalType.getDepositAmount());
        //查询图片
        Long descImgId = terminalType.getDescImgId();
        Picture picture = pictureService.findById(descImgId);
        map.put("descImgIdUrl", picture.getUrl());
        return map;
    }
}
